/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zip;

import aaa.Configurator;
import java.io.*;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 *
 * @author Bashev
 */
public class Unzip {
    
    public List<String> unzipFile(String file){
        Enumeration entries;
        ZipFile zipFile;
        List<String> result = new LinkedList<String>();
        try {
            int counter = 0; 
            zipFile = new ZipFile(file);
            entries = zipFile.entries();
            while(entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry)entries.nextElement();
                String path = Configurator.getConfigProperty("apk_output_path") + "\\" + entry.getName();
                if(entry.isDirectory()) {
                    // Assume directories are stored parents first then children.
                    System.err.println("Extracting directory: " + entry.getName());                    
                    // This is not robust, just for demonstration purposes.                    
                    (new File(path)).mkdir();
                    continue;
                }
                System.err.println("Extracting file: " + entry.getName());
                
                if (entry.getName().endsWith("en.lproj/Localizable.strings") || entry.getName().endsWith("English.lproj/Localizable.strings")) {
                    result.add(entry.getName());
                    
                }
                // if mac, do ugly stuff:
//                String osName = System.getProperty("os.name").toLowerCase();
//                if (osName.startsWith("mac os x")) {
//                    if (entry.getName().endsWith("Localizable.strings")) {
//                        System.err.println("\n\n\n entry.getName: "+entry.getName());
//                        String basicpath = getConfigProperty("xls_path"); 
//                        String mypath = basicpath + "/NetBeansProjects/AAA/"+entry.getName(); 
//                        System.err.println("mypath: "+mypath);
//                        counter = counter + 1; 
//                        String newpath = basicpath+"/stringsfile"+Integer.toString(counter)+".strings"; 
//                        copyfile(mypath, newpath); 
//                    }
//                }
                copyInputStream(zipFile.getInputStream(entry),
                new BufferedOutputStream(new FileOutputStream(path)));
            }            
        zipFile.close();
        return result;
        } catch (IOException ioe) {
            System.err.println("Unhandled exception:");
            ioe.printStackTrace();
            return result;
        }
    }
 public static String getConfigProperty(String propName) throws FileNotFoundException, IOException{
        Properties prop = new Properties();
        FileInputStream fis;
        try {
            fis = new FileInputStream("ConfigAAA.xml");
            prop.loadFromXML(fis);
            return prop.getProperty(propName, "");
        } catch (IOException ex) {
            return "";
        }     
    }
private static void copyfile(String srFile, String dtFile) throws FileNotFoundException, IOException{
  try   {
  File f1 = new File(srFile);
  File f2 = new File(dtFile);
   System.out.print("Beginning...f1: "+f1.toString()+" f2: "+f2.toString()); 
  InputStream in = new FileInputStream(f1);
  OutputStream out = new FileOutputStream(f2);

 
  //byte[] buf = new byte[1024];
  int len;
  while ((len = in.read()) != -1){
  out.write(len);
  }
  in.close();
  out.close();
  }
  finally {

  System.out.println("File copied.");
  }
 }
    public static final void copyInputStream(InputStream in, OutputStream out)throws IOException {
        byte[] buffer = new byte[1024];
        int len;
        while((len = in.read(buffer)) >= 0)
        out.write(buffer, 0, len);
        in.close();
        out.close();
    }
        
}

